package me.dz.dreamcleaner.manager;

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.maxgamer.QuickShop.Shop.ContainerShop;
import org.maxgamer.QuickShop.Shop.Shop;
import org.maxgamer.quickshop.QuickShop;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class ShopManager {

    public static Object shopManager;
    public static Type type;

    public static boolean isQuickShop(Item item){
        return type.isQuickShop(item);
    }

    public static void checkShop(){
        try {
            Class.forName("org.maxgamer.quickshop.QuickShop");
            QuickShop quickShop;
            try {
                quickShop = QuickShop.getInstance();
            }catch (Throwable ignored){
                quickShop = (QuickShop) QuickShop.class.getDeclaredField("instance").get(null);
            }
            try {
                shopManager = quickShop.getShopManager();
            }catch (Throwable ignored){
                shopManager = quickShop.getClass().getMethod("getShopManager").invoke(quickShop);
            }
            try {
                shopManager.getClass().getMethod("getShopInWorld",World.class);  
                type = Type.original;
            }catch (Throwable ignored){
                type = Type.deriver;
            }
        }catch (Throwable ignored){
            type = Type.reset;
            shopManager = org.maxgamer.QuickShop.QuickShop.instance.getShopManager();
        }
    }

    public enum Type{
        original{
            @Override
            public boolean isQuickShop(Item item) {
                try {
                    Map<?,?> map = (Map<?,?>) shopManager.getClass().getMethod("getShops", Chunk.class).invoke(shopManager,item.getLocation().getChunk());
                    if (map == null)return false;
                    Collection<?> shops = map.values();
                    if (shops.size() > 0) {
                        for (Object shop : shops) {
                            if ((boolean) shop.getClass().getMethod("isLoaded").invoke(shop)) {
                                Object play = shop.getClass().getMethod("getDisplay").invoke(shop);
                                Entity entity = (Entity) play.getClass().getMethod("getDisplay").invoke(play);
                                if (entity instanceof Item){
                                    Item display = (Item) entity;
                                    if (item == display) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    e.printStackTrace();
                }
                return false;
            }
        },
        deriver{
            @Override
            public boolean isQuickShop(Item item) {
                try {
                    Map<?,?> map = (Map<?,?>) shopManager.getClass().getMethod("getShops", Chunk.class).invoke(shopManager,item.getLocation().getChunk());
                    if (map == null)return false;
                    Collection<?> shops =  map.values();
                    if (shops.size() > 0){
                        for (Object shop : shops) {
                            Object play = shop.getClass().getMethod("getDisplayItem").invoke(shop);
                            Item display = (Item) play.getClass().getMethod("getItem").invoke(play);
                            if (item == display) {
                                return true;
                            }
                        }
                    }
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    e.printStackTrace();
                }
                return false;
            }
        },
        reset{
            @Override
            public boolean isQuickShop(Item item) {
                org.maxgamer.QuickShop.Shop.ShopManager manager = (org.maxgamer.QuickShop.Shop.ShopManager) shopManager;
                HashMap<Location, Shop> shopMap = manager.getShops(item.getLocation().getChunk());
                if (shopMap != null && shopMap.size() > 0){
                    for (Shop shop : shopMap.values()){
                        ContainerShop containerShop = (ContainerShop) shop;
                        if (containerShop.getDisplayItem() != null && item == containerShop.getDisplayItem().getItem()){
                            return true;
                        }
                    }
                }
                return false;
            }
        };

        public abstract boolean isQuickShop(Item item);
    }
}
